<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Function template sum</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Chapter 1. Boost.Histogram">
<link rel="up" href="../../../histogram/reference.html#header.boost.histogram.algorithm.sum_hpp" title="Header &lt;boost/histogram/algorithm/sum.hpp&gt;">
<link rel="prev" href="slice_an_idm45537378326496.html" title="Function slice_and_rebin">
<link rel="next" href="../axis/boolean.html" title="Class template boolean">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="slice_an_idm45537378326496.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../../histogram/reference.html#header.boost.histogram.algorithm.sum_hpp"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../axis/boolean.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="refentry">
<a name="boost.histogram.algorithm.sum"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2><span class="refentrytitle">Function template sum</span></h2>
<p>boost::histogram::algorithm::sum — Compute the sum over all histogram cells (underflow/overflow included by default). </p>
</div>
<h2 xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv-title">Synopsis</h2>
<div xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="refsynopsisdiv"><pre class="synopsis"><span class="comment">// In header: &lt;<a class="link" href="../../../histogram/reference.html#header.boost.histogram.algorithm.sum_hpp" title="Header &lt;boost/histogram/algorithm/sum.hpp&gt;">boost/histogram/algorithm/sum.hpp</a>&gt;

</span>
<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> A<span class="special">,</span> <span class="keyword">typename</span> S<span class="special">&gt;</span> 
  <span class="keyword">auto</span> <span class="identifier">sum</span><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../histogram.html" title="Class template histogram">histogram</a><span class="special">&lt;</span> <span class="identifier">A</span><span class="special">,</span> <span class="identifier">S</span> <span class="special">&gt;</span> <span class="special">&amp;</span> hist<span class="special">,</span> <span class="keyword">const</span> <span class="identifier">coverage</span> cov <span class="special">=</span> <span class="identifier">coverage</span><span class="special">::</span><span class="identifier">all</span><span class="special">)</span><span class="special">;</span></pre></div>
<div class="refsect1">
<a name="idm46096938719200"></a><h2>Description</h2>
<p>The implementation favors accuracy and protection against overflow over speed. If the value type of the histogram is an integral or floating point type, accumulators::sum&lt;double&gt; is used to compute the sum, else the original value type is used. Compilation fails, if the value type does not support operator+=. The return type is double if the value type of the histogram is integral or floating point, and the original value type otherwise.</p>
<p>If you need a different trade-off, you can write your own loop or use <code class="computeroutput">std::accumulate</code>:</p>
<pre class="programlisting"><span class="comment">// iterate over all bins</span>
<span class="keyword">auto</span> <span class="identifier">sum_all</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">accumulate</span><span class="special">(</span><span class="identifier">hist</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="identifier">hist</span><span class="special">.</span><span class="identifier">end</span><span class="special">(</span><span class="special">)</span><span class="special">,</span> <span class="number">0</span><span class="special">.</span><span class="number">0</span><span class="special">)</span><span class="special">;</span>

<span class="comment">// skip underflow/overflow bins</span>
<span class="keyword">double</span> <span class="identifier">sum</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
<span class="keyword">for</span> <span class="special">(</span><span class="keyword">auto</span><span class="special">&amp;&amp;</span> <span class="identifier">x</span> <span class="special">:</span> <span class="identifier">indexed</span><span class="special">(</span><span class="identifier">hist</span><span class="special">)</span><span class="special">)</span>
  <span class="identifier">sum</span> <span class="special">+=</span> <span class="special">*</span><span class="identifier">x</span><span class="special">;</span> <span class="comment">// dereference accessor</span>

<span class="comment">// or:</span>
<span class="comment">// auto ind = boost::histogram::indexed(hist);</span>
<span class="comment">// auto sum = std::accumulate(ind.begin(), ind.end(), 0.0);</span>
</pre>
<p>

</p>
<div class="variablelist"><table border="0" class="variablelist compact">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term">Parameters:</span></p></td>
<td><div class="variablelist"><table border="0" class="variablelist compact">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><code class="computeroutput">cov</code></span></p></td>
<td><p>Iterate over all or only inner bins (optional, default: all). </p></td>
</tr>
<tr>
<td><p><span class="term"><code class="computeroutput">hist</code></span></p></td>
<td><p>Const reference to the histogram. </p></td>
</tr>
</tbody>
</table></div></td>
</tr>
<tr>
<td><p><span class="term">Returns:</span></p></td>
<td><p>accumulator type or double</p></td>
</tr>
</tbody>
</table></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Hans
      Dembinski<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="slice_an_idm45537378326496.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../../histogram/reference.html#header.boost.histogram.algorithm.sum_hpp"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../axis/boolean.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
